package gdconf

import (
	"server_cluster/common/logger"
)

// 图鉴配置表

type CodexConfig struct {
	CodexId    int32 `csv:"Id" gorm:"primary_key;autoIncrement:false"` // 图鉴id
	CodexType  int32 `csv:"Type,omitempty"`                            // 图鉴类型
	OtherId    int32 `csv:"Name,omitempty"`                            // 关联id
	RewardItem *Item `csv:"Unlockreward,omitempty"`                    // 奖励道具
}

func (c *CodexConfig) TableName() string {
	return "config_codex"
}

func (g *GameDataConfig) saveCodexConfig() {
	saveTableToDb[CodexConfig](g.Db, readTable[CodexConfig](g.CsvPathPrefix+"Pictorial.csv"))
}

func (g *GameDataConfig) loadCodexConfig() {
	g.GameDataMaps.CodexConfigMap = make(map[int32]*CodexConfig)
	g.GameDataMaps.CodexConfigOtherIdMap = make(map[int32]*CodexConfig)
	for _, codexConfig := range loadTableFromDb[CodexConfig](g.Db) {
		g.GameDataMaps.CodexConfigMap[codexConfig.CodexId] = codexConfig
		g.GameDataMaps.CodexConfigOtherIdMap[codexConfig.OtherId] = codexConfig
	}
	logger.Info("CodexConfig Count: %v", len(g.GameDataMaps.CodexConfigMap))
}

func GetCodexConfig(codexId int32) *CodexConfig {
	value, exist := CONF.GameDataMaps.CodexConfigMap[codexId]
	if !exist {
		logger.Error("[ConfigNotFound] CodexConfig, codexId: %v", codexId)
		return nil
	}
	return value
}

func GetCodexConfigMap() map[int32]*CodexConfig {
	return CONF.GameDataMaps.CodexConfigMap
}

func GetCodexConfigByOtherId(otherId int32) *CodexConfig {
	value, exist := CONF.GameDataMaps.CodexConfigOtherIdMap[otherId]
	if !exist {
		logger.Error("[ConfigNotFound] CodexConfig, otherId: %v", otherId)
		return nil
	}
	return value
}
